import { createRouter, createWebHashHistory } from "vue-router";
import { constrouters } from "./routers";
import NProgress from 'nprogress';
import VueCookies from 'vue-cookies';
let router = createRouter({
    history: createWebHashHistory(),
    routes: constrouters,
    scrollBehavior() {
        return {
            top: 0,
            left: 0
        }
    }
})
const isAuthenticated = () => {
    // 这里可以添加你的逻辑来检查用户是否已登录
    const loginInfo = VueCookies.get('userInfo');
    return loginInfo !== undefined && loginInfo !== null && loginInfo !== '';
}
// 在路由跳转开始时显示进度条
router.beforeEach((to, from, next) => {
    NProgress.start();
    if (to.matched.some(record => record.meta.needLogin)) {
        if (!isAuthenticated()) {
            next({
                path: '/login',
                query: { redirect: to.fullPath }
            });
        } else {
            next();
        }
    } else {
        next();
    }
});

// 在路由跳转结束后隐藏进度条
router.afterEach(() => {
    NProgress.done();
});

export default router